package cvs.service;

import com.sun.xml.internal.bind.v2.model.core.ID;
import cvs.dao.BookingMapper;
import cvs.entity.Booking;
import cvs.utils.MyBatisUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class BookingServive {
    public List<Booking> getList(Integer pageIndex, Integer pageSize){
        SqlSession sqlSession = null;
        List<Booking> rentList = new ArrayList<Booking>();
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            rentList = sqlSession.getMapper(BookingMapper.class).getList(pageIndex,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return rentList;
    }
    public int delBooking (Integer id){
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(BookingMapper.class).delBooking(id);
            //            提交事务
            sqlSession.commit();
        }catch (Exception e) {
            e.printStackTrace();
            //            回滚事务
            sqlSession.rollback();
            count=0;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    public int addBooking(Booking booking){
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(BookingMapper.class).addBooking(booking);
            //            提交事务
            sqlSession.commit();
        }catch (Exception e) {
            e.printStackTrace();
            //            回滚事务
            sqlSession.rollback();
            count=0;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    public int modify(int id){
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(BookingMapper.class).modify(id);
            //            提交事务
            sqlSession.commit();
        }catch (Exception e) {
            e.printStackTrace();
            //            回滚事务
            sqlSession.rollback();
            count=0;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }
}
